// pages/item/item.js

const app=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:"",
    name: "",
    phone: "",
    conpany: "",
    hangye:  "",
    zhaoshang_demand: "",
    mianji: "",
    leixing: "",
    quyu: "",
    list:[],
    loading: true,
    beizhu:"",
    pingfangmi: "m²",
    status:"",
    sign:[],
    textcolor: ["#47f047", "#f7b466", "#858585", "#f24f3e", "#68c7f7", "#6a6ce8", "#d97cf6", "#5ff1c8", "#9fef58"],
    backcolor: ["#daf7da", "#f4ebd7", "#e4e4e4", "#f6d3cf", "#c1e3f4", "#b9ebea", "#edd7f4", "#cff6eb", "#d9f4c1",],
    isFenPei: false,
    isBoDa: false,
    isZhaoShang: false,
    created_at: "",
    adv_name: "",
    external_url: "",
    sea:0,
    can_follow:0,
    xuanze:[],
    xuanzeindex:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this
    this.setData({
      id: options.id,
      can_follow:options.can_follow,
      sea: options.sea,
      isFenPei: app.globalData.isCustomDetailsFenPei,
      isBoDa: app.globalData.isCustomDetailsBoDa,
    })
    if(this.data.sea==1&&this.data.can_follow==1){
      console.log('v2projecrt')
      wx.request({
        url: app.globalData.httpurl + 'v2/project',
        method: "GET",
        header: {
          "Authorization": "bearer " + app.globalData.access_token
        },
        data: {
        },
        success: function (res) {
          var adds = [{ "project_id": "", "name": "请选择领取到哪个项目" }];
          that.setData({
            xuanze: adds.concat(res.data.data)
          })
          for (var i = 0; i < that.data.xuanze.length; i++) {
            if (that.data.xuanze[i].project_id == options.project_id) {
              that.setData({
                xuanzeindex: i
              })
            }
          }
        }
      })
    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

    var that = this //很重要，一定要写
    that.setData({
      list:[],
      sign:[]
    })
    wx.request({
      url: app.globalData.httpurl + 'coustomer/detail',
      method: "GET",
      header: {
        "Authorization": "bearer " + app.globalData.access_token
      },
      data: {
        "customer_id": this.data.id,
        "sea":this.data.sea
      },
      success: function (res) {
        var datas = res.data.data; //res.data就是从后台接收到的值
        console.log(datas);
        for (var i = 0; i < datas.follow.length; i++) {
          datas.follow[i]["time"] = datas.follow[i]["created_at"].substring(11, 16)
          datas.follow[i]["created_at"] = datas.follow[i]["created_at"].substring(1, 10)
        }
        that.setData({
          name: datas.name,
          mobile: datas.mobile,
          company_name: datas.company_name,
          isZhaoShang: datas.project.name == '招商采盘' ,
          hangye: datas.industry == null ? "" : datas.industry.name,
          mianji: datas.area == null ? "" : datas.area.name,
          xuqiu: datas.demand == null ? "" : datas.demand.name,
          address: datas.city2,
          leixing: datas.project == null ? "" : datas.project.name,
          quyu: datas.source == null ? "" : datas.source.name,
          list: that.data.list.concat(datas.follow),
          loading: false,
          beizhu: datas.beizhu == null ? "" : datas.beizhu.desc,
          status: datas.follow_status,
          loading: false,
          zhaoshang_demand: datas.zhaoshang_demand == null ? "" : datas.zhaoshang_demand.name,
          created_at: datas.created_at,
          adv_name: datas.adv_name,
          
          external_url: datas.external_url,
          sign: datas.sign
        })
        // wx.stopPullDownRefresh();
      },
      fail: function (res) {
        console.log('submit fail');
      },
      complete: function (res) {
        console.log('submit complete');
      }
    })

    

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  bindshaixuan2: function (e) {
    console.log(e)
    if (e.detail.value == '0') {
      wx.showToast({
        title: '请选择一个项目',
        icon: 'none'
      })
      return
    }

    let project_id = this.data.xuanze[e.detail.value].project_id
 
    wx.request({
      url: app.globalData.httpurl + 'customer/receive',
      data: {
        customer_id: e.currentTarget.dataset.id,
        project_id
      },
      header: {
        "Authorization": "bearer " + app.globalData.access_token
      },
      method: 'POST',
      dataType: 'json',
      responseType: 'text',
      success: res => {
        if (res.data.code == 0) {
          var pages = getCurrentPages(); //当前页面
          var beforePage = pages[pages.length - 2]; //前一页
          console.log(beforePage)
          wx.navigateBack({
            success:  ()=> {
                  beforePage.RemoveCustomer(e.currentTarget.dataset.id); // 执行前一个页面的onLoad方法  
            }
          });
        } else {
          wx.showToast({
            title: res.data.msg,
          })
        }
      },
      fail: function (res) {
        wx.showToast({
          title: '网络出错',
          icon: 'none',

          success: function (res) { },
          fail: function (res) { },
          complete: function (res) { },
        })
      },
      complete: function (res) { },
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var that = this;
　　// 设置菜单中的转发按钮触发转发事件时的转发内容
　　var shareObj = {
      　　　　title: "昊信联行客户管理系统",        // 默认是小程序的名称(可以写slogan等)
      　　　　path: '/pages/index/index?type=3&id='+that.data.id,        // 默认是当前页面，必须是以‘/’开头的完整路径
      　　　　//imageUrl: '',     //自定义图片路径，可以是本地文件路径、代码包文件路径或者网络图片路径，支持PNG及JPG，不传入 imageUrl 则使用默认截图
        success: function (res) {
          // 转发成功之后的回调
          if (res.errMsg == 'shareAppMessage:ok') {
          }
        },
        fail: function () {
          // 转发失败之后的回调
          if (res.errMsg == 'shareAppMessage:fail cancel') {
            // 用户取消转发
          } else if (res.errMsg == 'shareAppMessage:fail') {
            // 转发失败，其中 detail message 为详细失败信息
          }
        },
        complete: function(){
          // 转发结束之后的回调（转发成不成功都会执行）
        }
  　　}
    // 返回shareObj
　　return shareObj;
  },
  bindchengjiao: function(){

  },
  bindno:function(){

  },
  binddetails:function(){
    wx.navigateTo({
      url: '/pages/write/write?id='+ this.data.id,
    })
  },
  bindPhone: function (e) {
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.dataset.mobile,
    })
  },
  bindfenpei: function (e) {
    wx.navigateTo({
      // url: '/pages/persionlist/persionlist?id=' + e.currentTarget.dataset.id,
      url: '/pages/persionlist/persionlist?id=' + this.data.id,
    })
  },
  home: function (e) {
    wx.navigateTo({
     
      url: "/pages/index/index"
    })
  },
  bindweb: function(e){
    wx.navigateTo({
      // url: '/pages/persionlist/persionlist?id=' + e.currentTarget.dataset.id,
      url: '/tongjiP/pages/web/web?url=www.baidu.com' + this.data.external_url ,
    })
  }
})